<?php
include("./config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	//mysql_real_escape_string : SQL Injection을 막기 위한 함수
	$register_id = mysql_real_escape_string($_POST['id']);
	$register_pw = mysql_real_escape_string($_POST['pw']);
	$mysql_date = date('Y-m-d');

	$sql = "SELECT * FROM USER_TABLE WHERE Id = LOWER('$register_id')";
	$result = mysql_query($sql);
	$count = mysql_num_rows($result);
	
	if($count > 0){
		echo 2;
		die();
	} // already exist

	$sql = "INSERT INTO USER_TABLE (Id,Passwd,Registerdate) 
	VALUES ('$register_id','$register_pw','$mysql_date')";

	$result = mysql_query($sql);
	$returnResult = 0;
	
	if ($result == '1') {
		$returnResult = 1;
	} else {
		$returnResult = -1;
	}

	// create user info
	$sql = "SELECT UserNum FROM USER_TABLE WHERE Id = '$register_id' and Passwd = '$register_pw'";
	$result = mysql_query($sql) or die('-1'); // 로그인 실패시 DIE(-1)
	$row = mysql_fetch_array($result);
	$userNum = $row['UserNum'];

	$sql = "insert into user_info_table (UserNum, CellStack) values ('$userNum',1000000)";
	mysql_query($sql) or die(-1);


	// create user item info
	for($i=1; $i<=3; $i++){
		$sql = "insert into user_item_table (UserNum, ItemTableNum, ItemNum) 
			values ('$userNum', $i, 0)";	
		$result = mysql_query($sql) or die(-1);
	}
	

	echo $returnResult;
}
?>